﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using CodeNotes.Model;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
namespace CodeNotes.BLL
{
    /// <summary>
    /// CW_Book
    /// </summary>
    public partial class CW_BookLogic
    {
        private readonly CodeNotes.DAL.CW_Book dal = new CodeNotes.DAL.CW_Book();
        public CW_BookLogic()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CodeNotes.Model.CW_Book model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CodeNotes.Model.CW_Book model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.CW_Book GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public CodeNotes.Model.CW_Book GetModelByCache(int Id)
        {

            string CacheKey = "CW_BookModel-" + Id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (CodeNotes.Model.CW_Book)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CodeNotes.Model.CW_Book> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CodeNotes.Model.CW_Book> DataTableToList(DataTable dt)
        {
            List<CodeNotes.Model.CW_Book> modelList = new List<CodeNotes.Model.CW_Book>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CodeNotes.Model.CW_Book model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 小说列表，从缓存中
        /// </summary>
        public List<CodeNotes.Model.CW_Book> GetModelListByCache(string strWhere,string strSort)
        {
  
            string CacheKey = "CW_BookModelList";
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            DataTable dtAllList = new DataTable();
            if (objModel == null)
            {
                try
                {
                    dtAllList = GetList(600,"IsShow=1","Sort desc,UpdateDate desc").Tables[0];
                    objModel = dtAllList;
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }                    
                }
                catch(Exception ex)
                {
                    throw (ex);
                }
            }
            else
            {
                dtAllList = (DataTable)objModel;
            }

            
            List<CW_Book> result = new List<CW_Book>();
            if(dtAllList.Rows.Count>0)
            {
                if(!string.IsNullOrEmpty(strSort))
                {
                    dtAllList.DefaultView.Sort = strSort;
                    dtAllList = dtAllList.DefaultView.ToTable();
                }
                
                DataRow[] drs = dtAllList.Select(strWhere);     
                result = DataRowsToList(drs);                
            }                            
            return result;
        }

        public List<CodeNotes.Model.CW_Book> DataRowsToList(DataRow[] drs)
        {
            List<CodeNotes.Model.CW_Book> modelList = new List<CodeNotes.Model.CW_Book>();
            int rowsCount = drs.Length;
            if (rowsCount > 0)
            {
                CodeNotes.Model.CW_Book model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(drs[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        public List<CodeNotes.Model.CW_Book> GetModelListByKeyword(string keyword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,BookName,Author,Descriotion,ClickCount,StarCount,IsOver,Source,CreateDate,UpdateDate,Sort,IsShow,Category  from CW_Book ");

            strSql.Append(" where (BookName like @keyword or Author like @keyword or Descriotion like @keyword) and IsShow=1 order by Sort desc,StarCount desc");
            SqlParameter[] parameters = {
					new SqlParameter("@keyword", SqlDbType.NVarChar)
			};
            parameters[0].Value = "%" + keyword + "%";
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);
        }

        public void addClickCount(int bookid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CW_Book set ClickCount=ClickCount+1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = bookid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        #endregion  ExtensionMethod
    }
}

